Assignment 2

Before working on this assignment please read these instructions fully. In the submission area, you will notice that you can click the link to Preview the Grading for each step of the assignment. This is the criteria that will be used for peer grading. Please familiarize yourself with the criteria before beginning the assignment.

An NOAA dataset has been stored in the file data/C2A2_data/BinnedCsvs_d400/fb441e62df2d58994928907a91895ec62c2c42e6cd075c2700843b89.csv. This is the dataset to use for this assignment. Note: The data for this assignment comes from a subset of The National Centers for Environmental Information (NCEI) Daily Global Historical Climatology Network (GHCN-Daily). The GHCN-Daily is comprised of daily climate records from thousands of land surface stations across the globe.

Each row in the assignment datafile corresponds to a single observation.

The following variables are provided to you:

  • id : station identification code
  • date : date in YYYY-MM-DD format (e.g. 2012-01-24 = January 24, 2012)
  • element : indicator of element type
    • TMAX : Maximum temperature (tenths of degrees C)
    • TMIN : Minimum temperature (tenths of degrees C)
  • value : data value for element (tenths of degrees C)

For this assignment, you must:

  1. Read the documentation and familiarize yourself with the dataset, then write some python code which returns a line graph of the record high and record low temperatures by day of the year over the period 2005-2014. The area between the record high and record low temperatures for each day should be shaded.
  2. Overlay a scatter of the 2015 data for any points (highs and lows) for which the ten year record (2005-2014) record high or record low was broken in 2015.
  3. Watch out for leap days (i.e. February 29th), it is reasonable to remove these points from the dataset for the purpose of this visualization.
  4. Make the visual nice! Leverage principles from the first module in this course when developing your solution. Consider issues such as legends, labels, and chart junk.

The data you have been given is near Ann Arbor, Michigan, United States, and the stations the data comes from are shown on the map below.

In [5]:
import matplotlib.pyplot as plt
import mplleaflet
import pandas as pd

def leaflet_plot_stations(binsize, hashid):

    df = pd.read_csv('data/C2A2_data/BinSize_d{}.csv'.format(binsize))
    station_locations_by_hash = df[df['hash'] == hashid]
    lons = station_locations_by_hash['LONGITUDE'].tolist()
    lats = station_locations_by_hash['LATITUDE'].tolist()
    plt.figure(figsize=(8,8))
    plt.scatter(lons, lats, c='r', alpha=0.7, s=200)
    return mplleaflet.display()
In [6]:
leaflet_plot_stations(400,'fb441e62df2d58994928907a91895ec62c2c42e6cd075c2700843b89')
Out[6]:
In [7]:
get_ipython().magic('matplotlib notebook')
import pandas as pd
import numpy as np
import datetime as dt
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from matplotlib.ticker import FormatStrFormatter

data = pd.read_csv('data/C2A2_data/BinnedCsvs_d25/391a2922ad597ba080f4b99dea6d62842562d64845ef5df1a384561e.csv')
data["Data_Value"] = data['Data_Value'].div(10)

data2015 = data.where(data['Date'].str.contains('2015')).dropna()
data2015['Date'] = data2015.Date.str[5:]

data['Date'] = data.Date.str[5:]

# Skip leap days
data = data.where(data['Date'] != '02-29')

# Find record highs and lows

record_high = data.groupby('Date')['Data_Value'].max()
record_low = data.groupby('Date')['Data_Value'].min()

high2015  = data2015.groupby('Date')['Data_Value'].max()
low2015 = data2015.groupby('Date')['Data_Value'].min()

# 

observation_dates = list(range(1,366))
x = np.linspace(1,365,365)
y = np.linspace(1,365,365)

record_high2015 = high2015[high2015 >= record_high.reindex_like(high2015)]
x = [n for n in range(0,365) if (high2015.iloc[n] >= record_high.iloc[n])]
record_low2015 = low2015[low2015 <= record_low.reindex_like(low2015)]
y = [n for n in range(0,365) if (low2015.iloc[n] <= record_low.iloc[n])]

# No record was broken in the year 2015 in the region near Noida, Uttar Pradesh, India
plt.figure(figsize=(12,10))

plt.gca().yaxis.set_major_formatter(FormatStrFormatter('%d$^{\circ}$C'))

ax1 = plt.gca()

ax1.set_title('Highest and Lowest Temperature Records Broken in 2015')

plt.scatter(x,record_high2015,s=100,c='red',zorder=2,alpha=0.7)
plt.scatter(y,record_low2015,s=100,c='blue',zorder=2,alpha=0.7)

plt.yticks(np.arange(-40, max(y), 10))

ax1.fill_between(observation_dates,record_high, record_low, facecolor='gray', alpha=0.5)    

for spine in plt.gca().spines.values():
    spine.set_visible(False)

months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
plt.xticks(np.linspace(0,365,13), months)

ax1.tick_params(axis=u'both', which=u'both',length=0)


plt.show()

plt.savefig('temperature_2005-2015.png')
In [ ]: